package com.ruoyi.system.service.impl;

import com.ruoyi.system.domain.ShouhouInfo;
import com.ruoyi.system.domain.ShouhouParts;
import com.ruoyi.system.domain.ShouhouRecord;
import com.ruoyi.system.domain.ShouhouWuliu;
import com.ruoyi.system.mapper.EpmShouhouMaintainMapper;
import com.ruoyi.system.service.IEpmShouhouMaintainService;
import org.apache.ibatis.session.ExecutorType;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;
import java.util.concurrent.TimeUnit;

@Service
@Transactional
public class EpmShouhouMaintainServiceImpl implements IEpmShouhouMaintainService {
    @Autowired
    private EpmShouhouMaintainMapper epmShouhouMaintainMapper;
    // mybatis工厂类，提供与数据库交互的会话
    @Autowired
    private SqlSessionFactory sqlSessionFactory;

    public List<ShouhouInfo> list(ShouhouInfo shouhouInfo) {
        return epmShouhouMaintainMapper.list(shouhouInfo);
    }


    /**
     * 新增售后商品
     *
     * @param shouhouInfo
     */
    @Transactional
    public void insertShouhouInfo(ShouhouInfo shouhouInfo) {
        // 默认状态:未维修状态
        shouhouInfo.setMachineCondition(1);
        shouhouInfo.setBackDate(new Date());
        shouhouInfo.setPartsInformation(shouhouInfo.getSnId());

        // 同时插入数据表和维修表，并设置为未维修状态
        epmShouhouMaintainMapper.insertShouhouInfo(shouhouInfo);
        epmShouhouMaintainMapper.insertShouhouWeixiu(shouhouInfo);
    }


    /**
     * 对商品进行维修,商品状态需要进行修改
     *
     * @param shouhouInfo
     */
    @Transactional
    public void updateShouhouWeixiu(ShouhouInfo shouhouInfo){
        // 更新完成时间
        shouhouInfo.setFinishTime(new Date());
        epmShouhouMaintainMapper.updateShouhouWeixiu(shouhouInfo);
        //切换为已维修状态
        epmShouhouMaintainMapper.updateMachineCondition(shouhouInfo.getSnId());
    }

    /**
     * 发货操作
     */

    // 发货操作
    @Transactional
    public void send(String snId) {

        // 锁住这条记录，防止并发操作
        epmShouhouMaintainMapper.lockShouhouInfo(snId);

//        获取到三张数据表的数据
        ShouhouRecord records = epmShouhouMaintainMapper.get(snId);
        records.setMachineCondition(3);
        records.setOperateType(1);
        records.setOperateTime(new Date());
        epmShouhouMaintainMapper.addRecordInformation(records);

//        删除两张表中的数据
        epmShouhouMaintainMapper.sendShouhouInfo(snId);
        epmShouhouMaintainMapper.sendShouhouWeixiu(snId);
    }

    public void insertShouhouWuliu(ShouhouWuliu shouhouWuliu){
        //        插入物流表信息
        shouhouWuliu.setTypeOfTable(1);
        epmShouhouMaintainMapper.insertShouhouWuliu(shouhouWuliu);
    }


    /**
     * 修改数据
     */
    public void update(ShouhouInfo shouhouInfo) {
        epmShouhouMaintainMapper.update(shouhouInfo);
    }



    /**
     * 更新七天失效情况（批量处理）
     */
    public void loadList(ShouhouInfo shouhouInfo) {

        // 查询数据库中的列表
        List<ShouhouInfo> list = epmShouhouMaintainMapper.list(shouhouInfo);

        // 获取当前日期
        Date currentDate = new Date();

        // 开启一个批量操作的 SqlSession
        //所有的数据库操作都会被暂时缓存在内存中，由commit提交给数据库
        try (SqlSession session = sqlSessionFactory.openSession(ExecutorType.BATCH)) {
            // 获取 Mapper
            EpmShouhouMaintainMapper batchMapper = session.getMapper(EpmShouhouMaintainMapper.class);

            // 遍历列表，计算每个 backDate 与当前日期的差异
            for (ShouhouInfo info : list) {
                if (info.getBackDate() != null) {
                    long diffInMillis = currentDate.getTime() - info.getBackDate().getTime();
                    long diffInDays = TimeUnit.DAYS.convert(diffInMillis, TimeUnit.MILLISECONDS);
                    int failureCondition = 7 - (int) diffInDays;
                    info.setFailureCondition(failureCondition);

                    // 执行批量更新操作
                    batchMapper.updateFailureCondition(info);
                }
            }

            // 提交批量更新
            session.commit();
        }
    }


    // 配件信息相关操作
    public List<ShouhouParts> partsQuery(String snId){
        return epmShouhouMaintainMapper.partsQuery(snId);
    }
    public void partsAdd(ShouhouParts shouhouParts){
        epmShouhouMaintainMapper.partsAdd(shouhouParts);
    }
    public void partsDelete(Integer id){
        epmShouhouMaintainMapper.partsDelete(id);
    }
}
